package com.hxd.otc.module.convenience.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.convenience.bean.ChooseHydropowerCityBean;
import com.hxd.otc.module.convenience.bean.NorGoodsBean;
import com.hxd.otc.module.convenience.bean.WaterCoalBillBean;
import com.hxd.otc.module.convenience.contract.PaymentInfoContract;
import com.hxd.otc.module.convenience.presenter.PaymentInfoPresenter;
import com.hxd.otc.utils.GlideUtils;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.TitleBar;

import butterknife.BindView;
import butterknife.OnClick;

public class HydropwerPaymentInfActivity extends BaseActivity<PaymentInfoPresenter> implements PaymentInfoContract.View {

    @BindView(R.id.title_bar)
    TitleBar titleBar;

    @BindView(R.id.tv_payment_unit)
    TextView tvPaymentUnit;

    @BindView(R.id.iv_icon)
    ImageView ivIcon;

    @BindView(R.id.tv_label)
    TextView tvLabel;

    @BindView(R.id.et_number)
    EditText etNumber;

    @BindView(R.id.bt_next)
    Button bt_next;

    @BindView(R.id.et_remarks)
    EditText etRemarks;

    NorGoodsBean goodsBean;
    ChooseHydropowerCityBean cityBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_hydropwer_payment_inf;
    }

    @Override
    protected void initView() {

        Bundle bundle = getIntent().getExtras();
        cityBean = (ChooseHydropowerCityBean) bundle.getSerializable("ChooseHydropowerCityBean");
        goodsBean = (NorGoodsBean) bundle.getSerializable(HydropwerDetailActivity.NORGOODSBEAN);

        titleBar.getRightText().setText(cityBean.city);
        tvPaymentUnit.setText(cityBean.rechargeUnit);
        tvLabel.setText(goodsBean.getGoodsName());
        GlideUtils.loadNormal(UIUtils.getContext(), UIUtils.getServiceImageUrlByPath(goodsBean.getGoodsIcon()), ivIcon);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected PaymentInfoPresenter createPresenter() {
        return new PaymentInfoPresenter(this);
    }

    @OnClick({R.id.iv_clear, R.id.bt_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_clear:
                etNumber.setText("");
                break;
            case R.id.bt_next:
                if (TextUtils.isEmpty(etNumber.getText().toString())) {
                    showToast(getString(R.string.enter_user_id_hint));
                    return;
                }
                bt_next.setEnabled(false);
                mPresenter.onSubmit(etNumber.getText().toString(), cityBean.itemId);
                break;
        }
    }

    @Override
    public void onSuccess(WaterCoalBillBean billBean) {
        bt_next.setEnabled(true);
        Bundle bundle = new Bundle();
        billBean.customerAddress = tvPaymentUnit.getText().toString();
        billBean.accountNo = etNumber.getText().toString();
        cityBean.remarks = etRemarks.getText().toString();

        bundle.putSerializable(HydropwerDetailActivity.NORGOODSBEAN, goodsBean);
        bundle.putSerializable("ChooseHydropowerCityBean", cityBean);
        bundle.putSerializable("billBean", billBean);
        $startActivity(HydropwerDetailActivity.class, bundle);
    }

    @Override
    public void onError(int code) {
        super.onError(code);
        bt_next.setEnabled(true);
    }
}
